<?php
/**
 * 角色校验类
 * Date: 2019/07/26
 */

namespace Lib\Paas;

use Lib\Base\Validate;

class Role extends Base
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取角色基本信息
     * @param array $data =
     * [
     *      'area' => '【必填】服务器ID（或者手游渠道：手Q、微信)',
     *      'platId' => '【业务区分平台时必填，否则选填】手机操作系统（iOS：0、安卓：1）',
     *      'partition' => '【业务区分区服时必填，否则选填】小区ID',
     *      'roleId' => '【如果需要校验openid和roleId的匹配关系时必填，否则选填】角色ID',
     *      'roleName' => '【选填】角色昵称(urlencode)',
     *      'sGameOpenid' => '【选填】游戏openid，查询非当前登录用户的角色信息时传递该值'
     * ]
     * @return array
     * [
     *      'iRet' => '=0 成功; !=0 失败',
     *      'sMsg' => '接口返回提示信息',
     *      'data' =>
     *          [
     *              'result' => '=0 成功, =1 游戏内未创建角色, !=0 && !=1 失败',
     *              ...
     *          ]
     * ]
     */
    public function getRoleInfo($data)
    {
        //校验角色相关信息是否合法
        $checkRet = Validate::checkRoleData($data);
        if ($checkRet['iRet'] != 0) {
            return $checkRet;
        }

        //url
        $url = $this->getPaasRequestUrl('gameattr.roleinfo', $data);
        $res = $this->http->get($url);

        recordLog("paas return roleinfo=" . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'gameattr.roleinfo');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];
        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            $result = [];
            $fields = $res['data'];
            foreach ($fields as $key => $value) {
                if (in_array($key, ['_idip_req_id_', '_idip_msg_id_'])) {
                    continue;
                }
                if (strpos($value, '|') !== false && !in_array($key, ['checkparam'])) {
                    $lists = explode('|', $value);
                    foreach ($lists as $index => $iteamList) {
                        if (!empty($iteamList)) {
                            if (strpos($iteamList, ' ') !== false) {
                                $iteams = explode(' ', $iteamList);
                                $result[$key][$index] = $iteams;
                            } else {
                                $result[$key][$index] = $iteamList;
                            }
                        }
                    }
                } else {
                    $result[$key] = $value;
                }
            }
            return $this->getRetData(0, 'success', ['data' => $result]);
        }
    }

    /**
     * 获取前端用于角色选择器的登录信息、签名、时间戳
     * @param string $game 需要生成签名的业务代码，默认为空，取当前业务
     * @return array
     * [
     *      'iRet' => '=0 成功; !=0 失败',
     *      'sMsg' => '接口返回提示信息',
     *      'data' => [
     *          'sSign' => '接口签名',
     *          'sCode' => '加密登录态',
     *          'timestamp' => '时间戳'
     *      ]
     * ]
     */
    public function getSecretInfo($game = '')
    {
        $signInfo = $this->getPaasSign('gameattr.nologinrole', $game);

        if ($signInfo === false) {
            return $this->getRetData('-1000300', '获取签名异常！');
        }

        $retData = $this->getLoginAuthInfo();
        $retData['sSign'] = $signInfo['sSign'];
        $retData['timestamp'] = $signInfo['timestamp'];

        return $this->getRetData('0', 'success', ['data' => $retData]);
    }
}
